<?php

namespace common\models\member;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%member_account}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $member_id 会员ID
 * @property int $amount 历史累积（分）
 * @property int $current 当前账户（分）
 * @property int $withdraw 历史提现总额（分）
 * @property int $last_draw 上次提现金额（分）
 * @property int $experience 成长值（点）
 * @property string $secret 支付密码
 * @property int $status 账户状态：-1账户冻结，0未激活，10正常
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class MemberAccount extends \common\db\ActiveRecord {

    use \common\traits\Constants;
    use \common\traits\relation\UserData;

    //可提现最小金额（分）
    const WITHDRAW_AMOUNT_LIMIT = 1;
//    const WITHDRAW_AMOUNT_LIMIT = 5000;
    //
    CONST STATUS_ACTIVE = 10;
    CONST STATUS_DEFAULT = 0;
    CONST STATUS_FREEZED = -1;
    CONST STATUS_FORBID = -2;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%member_account}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'member_id', 'amount', 'current', 'withdraw', 'last_draw', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['secret'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'member_id' => '会员ID',
            'amount' => '历史累积', //（分）
            'current' => '当前账户', //（分）
            'withdraw' => '历史提现总额', //（分）
            'last_draw' => '最近提现金额', //（分）
            'secret' => '支付密码',
            'status' => '账户状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
            'user' => '用户',
        ];
    }

    public function getMember() {
        return $this->hasOne(Member::class, ['id' => 'member_id']);
    }

    public function getLogs() {
        return $this->hasMany(MemberAccountLog::class, ['account_id' => 'id'])->orderBy([
            'created_at' => SORT_DESC
        ]);
    }

    /**
     * 是否激活账户
     *
     * @return bool
     */
    public function getIsActived() {
        return !$this->deleted_at && $this->status == static::STATUS_ACTIVE;
    }

    /**
     * 是否可用账户
     *
     * @return bool
     */
    public function getIsAvaild() {
        return !$this->deleted_at && $this->status >= static::STATUS_DEFAULT;
    }

    public function getConsoles() {
        $actions = [
            'ban' => [
                'label' => '<i class="fas fa-ban"></i>封禁帐户',
                'url' => ['/finance/account/ban', 'id' => $this->id],
                'options' => [
                    'class' => 'btn-danger',
                    'data-method' => 'POST',
                    'data-confirm' => '封禁后将永久解除达人账户，确认封禁？',
                ],
            ],
            'frozen' => [
                'label' => '<i class="fas fa-stop-circle"></i>冻结帐户',
                'url' => ['/finance/account/frozen', 'id' => $this->id],
                'options' => [
                    'class' => 'btn-light-danger',
                    'data-method' => 'POST',
                    'data-confirm' => '冻结后达人账户将不可使用提现、分佣等佣金功能，确定要冻结账户？',
                ],
            ],
            'unfreeze' => [
                'label' => '<i class="fas fa-share-square"></i> 解除冻结',
                'url' => ['/finance/account/unfreeze', 'id' => $this->id],
                'options' => [
                    'class' => 'btn-light-primary',
                    'data-method' => 'POST',
                    'data-confirm' => '解除冻结后达人账户将恢复可用状态，确认解冻？',
                ],
            ],
            'active' => [
                'label' => '<i class="fas fa-check-circle"></i>激活账户',
                'url' => ['/finance/account/active', 'id' => $this->id],
                'options' => [
                    'class' => 'btn-light-primary',
                    'data-method' => 'POST',
                    'data-confirm' => '激活后达人账户将正常收入佣金，确定要激活？',
                ],
            ],
            'back' => [
                'label' => '<i class="fas fa-reply"></i>返回账户列表',
                'url' => ['/finance/account'],
            ],
        ];
        $enables = [];
        if ($this->status == static::STATUS_DEFAULT) {
            $enables = [
                'ban',
                'frozen',
                'active',
                'back',
            ];
        } elseif ($this->status == static::STATUS_ACTIVE) {
            $enables = [
                'ban',
                'frozen',
                'back',
            ];
        } elseif ($this->status == static::STATUS_FORBID) {
            $enables = [
                'back',
            ];
        } elseif ($this->status == static::STATUS_FREEZED) {
            $enables = [
                'unfreeze',
                'back',
            ];
        } else {
            $enables = [
                'back',
            ];
        }
        $consoles = [];
        foreach ($enables as $console) {
            $action = ArrayHelper::getValue($actions, $console);
            $action && $consoles[] = $action;
        }
        return $consoles;
    }

}
